package com.leyou.search.listeners;

import com.leyou.search.service.IndexService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Listener {

    @Autowired
    private IndexService indexService;

    //声明队列，队列绑定给交换机，绑定时候要写绑定的routingKey

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.up.queue", durable = "true"),
            exchange = @Exchange(name = "leyou.item", type = ExchangeTypes.TOPIC),
            key = "item.up"))
    public void up(Long id) {
        this.indexService.crateIndex(id);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.down.queue", durable = "true"),
            exchange = @Exchange(name = "leyou.item", type = ExchangeTypes.TOPIC),
            key = "item.down"))
    public void down(Long id) {

        this.indexService.deleteIndex(id);
    }
}
